package com.shop.goods.controller;

import com.github.pagehelper.Page;
import com.shop.domain.Brand;
import com.shop.entity.PageResult;
import com.shop.entity.Result;
import com.shop.entity.StatusCode;

import com.shop.goods.service.BrandService;
import com.shop.util.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Brand")

public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private TokenDecode tokenDecode;
    /**
     * 查询全部品牌表
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    //@PreAuthorize("hasAnyAuthority('b')")
    //方法权限控制 有权限b的user才能使用此方法
     public Result<List<Brand>> selectAll(){
        //动态获取当前登录人
      //  Map<String, String> userInfo = tokenDecode.getUserInfo();
        return Result.ok("查询成功",brandService.selectAll());
    }

    /**
     * 新增
     * @param brand
     * @return
     */
    @PostMapping("/insert")
    public Result<Brand> insert(@RequestBody Brand brand){
        brandService.insert(brand);
        return Result.ok("新增成功");
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result<Brand> delete(Long id){
        brandService.delete(id);
        return Result.ok("删除成功");
    }

    /**
     * 更新品牌
     * @param brand
     * @return
     */
    @PostMapping("/update")
    public Result<Brand> update(@RequestBody Brand brand){
        brandService.update(brand);
        return Result.ok("更新成功");
    }

    /**
     * 品牌启用停用
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status")
    public Result<Brand> status(@RequestParam Long id,@RequestParam Integer status){
        if(id==null){
            return new Result<>(false, StatusCode.ERROR,"查询不到品牌");
        }
        Integer integer = null;
        if (status == null) {
            integer = brandService.status(id);
        } else {
            integer = brandService.status(id, status);
        }
        if (integer != 1) {
            return new Result<>(false, StatusCode.ERROR, "品牌状态修改失败");
        }
        return new Result<>(true, StatusCode.OK, "品牌状态修改成功");

    }

    /**
     * 条件查询
     * @param brand
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<Brand>> selectList(@RequestBody Brand brand){
        return new Result<>("查询成功",brandService.selectList(brand));
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findPage(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 条件分页查询
     * @param brand
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestParam Brand brand,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Brand> pageList = brandService.pageList(brand,page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

}
